package com.heima.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.PageResponse;
import com.heima.entity.TbPartner;
import com.heima.entity.TbVendingMachine;
import com.heima.mapper.TbPartnerMapper;
import com.heima.mapper.TbVendingMachineMapper;
import com.heima.pojo.vo.TbPartnerVo;
import com.heima.service.ITbPartnerService;
import com.heima.service.ITbVendingMachineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 合作商表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-06-08
 */
@RestController
@RequestMapping("/api/user-service")
@Slf4j
public class TbPartnerController {

    @Autowired
    private ITbPartnerService iTbPartnerService;

    @Autowired
    private TbPartnerMapper tbPartnerMapper;

    @Autowired
    private ITbVendingMachineService iTbVendingMachineService;

    @Autowired
    private TbVendingMachineMapper tbVendingMachineMapper;

    @PostMapping("/partner")
    public Boolean addPartner(@RequestBody TbPartner tbPartner){
        TbPartner tbPartner1 = new TbPartner();

        tbPartner1.setName(tbPartner.getName());
        tbPartner1.setContact(tbPartner.getContact());
        tbPartner1.setMobile(tbPartner.getMobile());
        tbPartner1.setRatio(tbPartner.getRatio());
        tbPartner1.setAccount(tbPartner.getAccount());
        tbPartner1.setPassword(BCrypt.hashpw(tbPartner.getPassword()));
        int row = tbPartnerMapper.insert(tbPartner1);
        if (row > 0) {
            return true;
        }
        return false;
    }

    @GetMapping("/partner/search")
    public PageResponse<TbPartnerVo> selectPartner(@RequestParam(value = "pageIndex",required = false,defaultValue = "1") Integer pageIndex,
                                                   @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize,
                                                   @RequestParam(value = "name",required = false) String name){
        //构建分页参数
        Page<TbPartner> page = new Page<>(pageIndex,pageSize);

        QueryWrapper<TbPartner> wrapper = new QueryWrapper<>();
        if (name != null) {
            wrapper.like("name",name);
        }
        //执行分页
        iTbPartnerService.page(page,wrapper);


        List<TbPartnerVo> partnerVos = page.getRecords().stream().map(r -> {
            String name1 = r.getName();
            Integer count = iTbVendingMachineService.lambdaQuery()
                    .like(name1 != null, TbVendingMachine::getOwnerName, name1)
                    .count();
            TbPartnerVo tbPartnerVo = new TbPartnerVo();
            tbPartnerVo.setVmCount(count);
            BeanUtil.copyProperties(r,tbPartnerVo);
            return tbPartnerVo;
        }).collect(Collectors.toList());


        PageResponse<TbPartnerVo> response = new PageResponse<>();
        response.setPageIndex(pageIndex);
        response.setPageSize(pageSize);
        response.setTotalCount((int) page.getTotal());
        response.setCurrentPageRecords(partnerVos);
        return response;
    }

    @GetMapping("/partner/{id}")
    public TbPartnerVo getVender(@PathVariable("id") Integer id){

        QueryWrapper<TbPartner> wrapper = new QueryWrapper<TbPartner>()
                .select("id", "name", "account", "ratio", "contact", "phone", "mobile")
                .eq("id", id);
        TbPartner tbPartner = tbPartnerMapper.selectById(wrapper);
        String name = tbPartner.getName();
        Integer count = iTbVendingMachineService.lambdaQuery()
                .like(name != null, TbVendingMachine::getOwnerName, name)
                .count();
        TbPartnerVo tbPartnerVo = new TbPartnerVo();
        BeanUtil.copyProperties(tbPartner,tbPartnerVo);
        tbPartnerVo.setVmCount(count);
        return tbPartnerVo;
    }
    @PutMapping("/partner/{id}")
    public Boolean updateVender(@PathVariable("id") Integer id,
                                @RequestBody TbPartner tbPartner){
        log.info("修改合作商");
        TbPartner tbPartner1 = new TbPartner();
        tbPartner1.setId(id);
        tbPartner1.setName(tbPartner.getName());
        tbPartner1.setContact(tbPartner.getContact());
        tbPartner1.setMobile(tbPartner.getMobile());
        tbPartner1.setRatio(tbPartner.getRatio());
        tbPartner1.setAccount(tbPartner.getAccount());

        int row = tbPartnerMapper.updateById(tbPartner1);
        if (row > 0) {
            return true;
        }
        return false;
    }

    @DeleteMapping("/partner/{id}")
    public Boolean deleteVender(@PathVariable("id") Integer id){
        int row = tbPartnerMapper.deleteById(id);
        if (row > 0) {
            return true;
        }
        return false;
    }
}
